package Utils;

import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;

import Utils.upload.Section;


public class ImageUtils {

	public static String uploadImage(Hashtable uploads, String folder) {
		// Fields
		String ret = "";
		/* get image file names */
		Enumeration enumers = uploads.elements();
		while (enumers.hasMoreElements()) {
			String imageName = "";
			Section section = (Section) enumers.nextElement();
			if (section.isFile()&&section.getName().indexOf("homepic")<0) {
				try {
					imageName = section.getSavedFileName();
					String newImageName = Random.randomNumber(5);
					String ext = imageName.substring(imageName
							.lastIndexOf("."));
					if(Common.checkImg(imageName)){
						if (new Thumbnail().ThumbnailImage(imageName,
								folder +Constant.FILE_SEPARATOR+ newImageName + ext,
								Constant.IMAGE_NORMAL_THUMBHEIGHT,
								Constant.IMAGE_NORMAL_THUMBWIDTH)) {
							
							ret = ret + "," + newImageName  + ext;
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
				} finally {
				}
			}
		}
		if (!ret.equals("")) {
			ret = ret.substring(1, ret.length());
		}
		return ret;
	}
	
	public static String uploadImageClip(Hashtable uploads, String folder) {
		// Fields
		String ret = "";
		/* get image file names */
		Enumeration enumers = uploads.elements();
		while (enumers.hasMoreElements()) {
			String imageName = "";
			Section section = (Section) enumers.nextElement();
			if (section.isFile()) {
				try {
					imageName = section.getSavedFileName();
					String newImageName = Random.randomNumber(5);
					String ext = imageName.substring(imageName
							.lastIndexOf("."));
					if(Common.checkImg(imageName)){
						if (new Thumbnail().ThumbnailImage(imageName,
								folder +Constant.FILE_SEPARATOR+ newImageName + ext,
								Constant.IMAGE_NORMAL_THUMBHEIGHT,
								Constant.IMAGE_NORMAL_THUMBWIDTH)) {
							if (new Thumbnail().ThumbnailImage(imageName,
									folder +Constant.FILE_SEPARATOR+ newImageName +"thumb"+ ext,
									Constant.IMAGE_SMALL_THUMBWIDTH,
									Constant.IMAGE_SMALL_THUMBWIDTH)){ 
								ret = ret + "," + newImageName  + ext;
							}
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
				} finally {
				}
			}
		}
		if (!ret.equals("")) {
			ret = ret.substring(1, ret.length());
		}
		return ret;
	}
	
	
	public static String uploadHomePic(String remain,Hashtable uploads, String folder) {
		// Fields
		String ret = "";
		/* get image file names */
		Enumeration enumers = uploads.elements();
		String newImageName = "";
		String ext = "";
		while (enumers.hasMoreElements()) {
			String imageName = "";
			Section section = (Section) enumers.nextElement();
			if (section.isFile()&&section.getName().indexOf("homepic")>=0) {
				try {
					newImageName = Random.randomString(5);
					imageName = section.getSavedFileName();
					ext = imageName.substring(imageName.lastIndexOf("."));
					if(Common.checkImg(imageName)){
						if (new Thumbnail().ThumbnailImage(imageName,
								folder +Constant.FILE_SEPARATOR+ newImageName + ext,
								Constant.IMAGE_NORMAL_THUMBHEIGHT,
								Constant.IMAGE_NORMAL_THUMBWIDTH)) {
							
							ret = ret + "," + newImageName  + ext;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
				}
			}
		}
		if (!ret.equals("")) {
			ret = ret.substring(1, ret.length());
		}
		if(!remain.equals("")){
			if(!ret.equals(""))
				ret = remain+","+ret;
		}
		return ret;
	}
	
	
	
	
	public static String downloadPic(String listLink, String path){
		String []list = listLink.split(",");
		String ret = "";
		
		for(int i=0; i < list.length; i++){
			String filename = Random.randomString(5);
			if(!list[i].equals("")){
				String dfilename = Download.download(list[i], path, filename);
				if(!dfilename.equals(""))
					ret += dfilename+",";
			}	
		}
		if(!ret.equals("")){
			ret = ret.substring(0, ret.length()-1);
		}
		return ret;
		
	}
	
	public static String uploadClip(Hashtable uploads, String folder) {
		Enumeration enumers = uploads.elements();
		String newImageName = "";
		String ext = "";
		while (enumers.hasMoreElements()) {
			String imageName = "";
			Section section = (Section) enumers.nextElement();
			if (section.isFile()) {
				try {
					newImageName = Random.randomString(5);
					imageName = section.getSavedFileName();
					ext = imageName.substring(imageName.lastIndexOf("."));
					if(ext.indexOf(".flv")>=0 || ext.indexOf(".mp4")>=0 || ext.indexOf(".mp3")>=0){
						String path = folder+"/"+newImageName + ext;
						if (FileUtils.copyFile(new File(section.getSavedFileName()), new File(path) ));
							return newImageName + ext;
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
				}
			}
		}
		return "";
	}
	
	
	
}
